<template>
  <div class="container">
    <Breadcrumb :items="['用户管理', '用户赛事']" />
    <div class="">
      <div class="header">
        <a-space direction="vertical" size="large">
          <a-input-search 
            :style="{width:'360px', marginLeft: '20px'}" 
            placeholder="Please enter something" 
            search-button 
            allow-clear
            v-model="searchValue"
            @search="handleSearch"
            @clear="searchClear"
          />
        </a-space>
      </div>
      <a-table :data="data" :pagination="false">
        <template #columns>
          <a-table-column title="ID" data-index="userMatchId"></a-table-column>
          <a-table-column title="赛事" data-index="matchTitle"></a-table-column>
          <a-table-column title="类型" data-index="matchType"></a-table-column>
          <a-table-column title="方式" data-index="raceMethod"></a-table-column>
          <a-table-column title="开始时间" data-index="startTime"></a-table-column>
          <a-table-column title="结束时间" data-index="endTime"></a-table-column>
          <a-table-column title="用户名" data-index="nickName"></a-table-column>
          <a-table-column title="邮箱" data-index="email"></a-table-column>
          <a-table-column title="学校" data-index="schoolName"></a-table-column>
          <a-table-column title="申请时间" data-index="updateTime"></a-table-column>
          <a-table-column title="状态" data-index="approveStatus"></a-table-column>
          <a-table-column title="操作" data-index="operation">
            <template #cell="{ record }">
              <a-space v-if="record.approveStatus === '待审核'">
                <a-button type="primary" status="success" size="small" shape="round" @click="handleGetSubmit(record)">通过</a-button>
                <a-button type="primary" status="danger" size="small" shape="round" @click="handlePassSubmit(record)">拒绝</a-button>
              </a-space>
              <a-space>
                <a-popconfirm content="是否更改为审核不通过?" position="bottom" type="warning" @ok="handleTGSubmit(record)">
                  <a-button v-if="record.approveStatus === '审核通过'"
                    type="primary" size="small" shape="round">重新审核</a-button>
                </a-popconfirm>
                <a-popconfirm content="是否更改为审核通过?" position="bottom" type="warning" @ok="handleUpdateSubmit(record)">
                  <a-button v-if="record.approveStatus === '审核不通过'"
                    type="primary" status="warning" size="small" shape="round">重新审核</a-button>
                </a-popconfirm>
              </a-space>
              <a-space v-if="record.approveStatus === '选手退赛'">
                <a-button type="primary" status="danger" size="small" shape="round" disabled>选手退赛</a-button>
              </a-space>
            </template>
          </a-table-column>
        </template>
      </a-table>
      <div style="width: 100%;margin: 10px auto;">
        <a-pagination @change="pageChange" :total="total" :current="curPage" :page-size="pageSize" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { Message } from '@arco-design/web-vue';
  import axios from 'axios';
  import { onMounted, ref } from 'vue';

  const data = ref([] as any);
  const curPage = ref(1)
  const pageSize = ref(8)
  const total = ref(1)
  const searchData = ref({
    searchField:'approve_status',
  })
  const searchValue = ref()
  const getData = () => {
    data.value = []
    axios.get(`userMatch/list`, {
      params: {
        curPage: curPage.value,
        pageSize: pageSize.value,
        searchField: searchData.value.searchField,
        search: searchValue.value,
      }
    }).then(res => {
      console.log(res)
      curPage.value = res.current
      pageSize.value = res.size
      total.value = res.total
      res.records.forEach((element:any) => {
        element.matchType = element.matchType === 1 ? '积分赛' : '常规赛'
        element.raceMethod = element.raceMethod === 1 ? '五次取平均' : '全部取平均'
        let approveStatusText = '';
        switch (element.approveStatus) {
          case 0:
            approveStatusText = '待审核';
            break;
          case 1:
            approveStatusText = '审核通过';
            break;
          case 2:
            approveStatusText = '选手退赛';
            break;
          case 3:
            approveStatusText = '审核不通过';
            break;
          default:
            break;
        }
        element.approveStatus = approveStatusText;
        data.value.push(element)
      });
    })
  }
  onMounted(async() => {
    await getData()
  })
  const handleGetSubmit = (record:any) => {
    axios.put(`/userMatch/approved/${record.userMatchId}/${1}`).then(res=>{
      console.log(res)
      getData()
    })
  }
  const handlePassSubmit = (record:any) => {
    axios.put(`/userMatch/approved/${record.userMatchId}/${3}`).then(res=>{
      console.log(res)
      getData()
    })
  }
  const handleTGSubmit = (record:any) => {
    axios.put(`/userMatch/approved/${record.userMatchId}/${3}`).then(res=>{
      console.log(res)
      getData()
    })
  }
  const handleUpdateSubmit = (record:any) => {
    axios.put(`/userMatch/approved/${record.userMatchId}/${1}`).then(res=>{
      console.log(res)
      getData()
    })
  }
  const pageChange = (current:number) => {
    curPage.value = current
    getData()
  }
  const handleSearch = () => {
    getData();
  }
  const searchClear = () => {
    getData()
  }
</script>

<style scoped lang="less">
  .container {
    padding: 0 20px 20px 20px;
  }

  .header {
    display: flex;
    padding: 20px;
    align-items: center;
    margin-bottom: 10px;
    background-color: #fff;
  }
</style>

<style lang="less" scoped>
  .mobile {
    .content {
      display: block;
      &-left {
        margin-right: 0;
        margin-bottom: 16px;
      }
      &-right {
        width: 100%;
      }
    }
  }
</style>

